package com.devilvan.betrice.brand.controller;

import com.devilvan.betrice.brand.po.CnBrandPo;
import com.devilvan.betrice.brand.service.CnBrandService;
import com.devilvan.betrice.brand.vo.CnBrandVo;
import com.devilvan.betrice.common.BetriceResultVo;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author Evad.Wu
 * @Description 中国品牌网 控制器
 * https://www.chinapp.com/paihang
 * @date 2022-09-16
 */
@RestController
@RequestMapping(value = "cnBrandController")
@Tag(name = "中国品牌网 控制器")
public class CnBrandController {
    @Resource(name = "cnBrandServiceImpl")
    private CnBrandService cnBrandService;

    /**
     * 步骤一：将网页的源码保存下来，然后后台采集并保存
     *
     * @return 形式回调
     */
    @Operation(summary = "步骤一：将网页的源码保存下来，然后后台采集并保存")
    @GetMapping(value = "doCnBrandCrawler")
    public BetriceResultVo<CnBrandPo> doCnBrandCrawler() {
        List<CnBrandVo> cnBrandVoList = cnBrandService.doCrawlViewSource();
        return cnBrandService.saveCnBrandBatch(cnBrandVoList);
    }

    /**
     * 步骤二：对已保存的采集数据进行业务处理
     *
     * @return 形式回调
     */
    @XxlJob(value = "doCnBrand")
    @Operation(summary = "步骤二：对已保存的采集数据进行业务处理")
    @GetMapping(value = "doCnBrand")
    public BetriceResultVo<CnBrandPo> doCnBrand() {
        BetriceResultVo<CnBrandPo> betriceResultVo = null;
        try {
            betriceResultVo = cnBrandService.doCnBrand().get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return betriceResultVo;
    }

    /**
     * 步骤三（异常队列处理）：对已保存的采集数据进行业务处理
     *
     * @return 形式回调
     */
    @Operation(summary = "步骤三（异常队列处理）：对已保存的采集数据进行业务处理")
    @GetMapping(value = "doCnBrandException")
    public BetriceResultVo<CnBrandPo> doCnBrandException() {
        return cnBrandService.doCnBrandException();
    }
}
